package com.wsz;

import com.wsz.pojo.Demo;
import com.wsz.pojo.User;
import com.wsz.utils.MysqlUtils;

import javax.rmi.CORBA.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        MysqlUtils utils = new MysqlUtils();
    }

    public static void find(MysqlUtils utils){
        System.out.println("==========查询任意表单列===========");
        String Sql = "select * from user1 where id = ?";
        User user = utils.TQuery(User.class, Sql, 1);
        System.out.println(user);
    }
    public static void findAll(MysqlUtils utils){
        System.out.println("==========查询任意表多列===========");
        String Sql1 = "select * from user1 where id > ?";
        List<User> users = utils.TAQuery(User.class, Sql1, 0);
        users.forEach(System.out::println);
    }

    public static void insert(MysqlUtils utils) {
        System.out.println("==========增加一条表记录(USer)===========");
        String Sql2 = "insert into user1(username,password,enabled,locked) values(?,?,?,?)";
        String username = "wsz";
        String password = "{noop}123";
        Boolean enabled = true;
        Boolean locked = false;
        utils.TUpdate(User.class,Sql2,username,password,enabled,locked);
    }
    public static void update(MysqlUtils utils) {
        System.out.println("==========修改一条表记录===========");
        String Sql3 = "update user1 set enabled = ? where username = ?";
        String username = "wsz";
        Boolean enabled = false;
        utils.TUpdate(User.class, Sql3, enabled, username);
        findAll(utils);
    }
    public static void delete(MysqlUtils utils) {
        System.out.println("==========删除一条表记录(User)===========");
        String Sql4 = "delete from user1 where id = ?";
        utils.TUpdate(User.class,Sql4,1);

        findAll(utils);
    }

    public static void CreateTable(MysqlUtils utils,String tableName,Class cla){
        System.out.println("==========创建一个表===========");
        Boolean flag = utils.CreateTable(tableName, cla);

        if(flag == true){
            System.out.println("创建表成功");
        }else{
            System.out.println("创建表失败");
        }
    }
    public static void DropTable(MysqlUtils utils,String tableName){
        System.out.println("==========删除一个表===========");
        Boolean flag = utils.dropTable(tableName);
        if(flag == true){
            System.out.println("删除表成功");
        }else{
            System.out.println("删除表失败");
        }
    }

    public static void CreateDemoTable(MysqlUtils utils){
        System.out.println("==========创建一个表(ert)===========");
        utils.CreateTable("ert",Demo.class);
    }

    public static void insertDemo(MysqlUtils utils){
        System.out.println("==========增加一条表记录(Demo)===========");
        String sql = "insert into ert(map,age,name,animal) values(?,?,?,?)";

        Map<String,String> map = new HashMap<>();
        map.put("i","wsz");
        map.put("you","fxc");

        Integer age = 18;

        String name = "wsz";

        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        utils.TUpdate(Demo.class, sql, map, age, name, list);
    }

    public static void findAllDemo(MysqlUtils utils){
        System.out.println("==========查询任意表多列(Demo)===========");
        String sql2 = "select * from ert where id > ?";
        List<Demo> demos = utils.TAQuery(Demo.class, sql2, 0);
        demos.forEach(System.out::println);
    }

    public static void findDemo(MysqlUtils utils){
        System.out.println("==========查询任意表单列(Demo)===========");
        String sql2 = "select * from ert where id = ?";
        List<Demo> demos = utils.TAQuery(Demo.class, sql2, 2);
        System.out.println(demos);
    }

    public static void updateDemo(MysqlUtils utils) {
        System.out.println("==========修改一条表记录===========");
        String Sql3 = "update ert set map = ?, age = ?, name = ?, animal = ? where id = ?";

        Map<String,String> map = new HashMap<>();

        Integer id = 1;

        map.put("i","xxx");
        map.put("you","fff");

        Integer age = 11;

        String name = "xxx";

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        Demo demo = utils.TUpdate(Demo.class, Sql3, map, age, name, list, id);
    }

    public static void deleteDemo(MysqlUtils utils) {
        System.out.println("==========删除一条表记录(User)===========");
        String Sql4 = "delete from ert where id = ?";
        utils.TUpdate(User.class,Sql4,4);
    }

    public static void CreateTables(MysqlUtils utils){
        System.out.println("多表创建");
        Boolean ausa = utils.CreateTable("ausa", Demo.class);
    }
}
